import base64
from django import http
import pickle
from django_redis import get_redis_connection

from meiduo_mall.utils.response_code import RETCODE


def merge_carts_cookies_to_redis(request,user,response):
    # 接收参数校验参数
    # carts_str = request.COOKIES.get('carts')
    if not request.COOKIES.get('carts'):
        return response

    carts_dict = pickle.loads(base64.b64decode(request.COOKIES.get('carts').encode()))

    new_cart_dict = {}

    pl = get_redis_connection('carts').pipeline()
    # pl = redis_conn.pipeline()

    for sku_id, sku in carts_dict.items():
        if sku['selected']:
            new_cart_dict[sku_id]=sku['count']    #   这句代码最重要
            pl.srem("selected_%s" % user.id, sku_id)
            pl.sadd('selected_%s' % user.id, sku_id)
        else:
            pl.srem("selected_%s" % user.id, sku_id)
            pl.hmset('carts_%s' % user.id, new_cart_dict)
    pl.execute()
    response.delete_cookie('carts')

    return response

    #
    #
    #

    #
    #
    # cart_str = request.COOKIES.get('carts')
    #
    # # 判断cookies中的购物车数据是否存在
    # if not cart_str:
    #     return response
    #
    # # 将 cart_str转成bytes类型的字符串
    # cookie_cart_str_bytes = cart_str.encode()
    # # 将cart_str_bytes转成bytes类型的字典
    # cookie_cart_dict_bytes = base64.b64decode(cookie_cart_str_bytes)
    # # 将cart_dict_bytes转成真正的字典
    # cookie_cart_dict = pickle.loads(cookie_cart_dict_bytes)
    #
    # """
    # {
    #     "sku_id1":{
    #         "count":"1",
    #         "selected":"True"
    #     },
    #     "sku_id3":{
    #         "count":"3",
    #         "selected":"False"
    #     }
    # }
    # """
    #
    # # 准备新的数据容器：保存新的sku_id:count、selected、unselected
    # new_cart_dict = {}
    # new_selected_add = []
    # new_selected_rem = []
    #
    # # 遍历出cookies中的购物车数据
    # for sku_id, cookie_dict in cookie_cart_dict.items():
    #
    #     # 错误的数据结构
    #     # new_cart_dict[sku_id] = {
    #     #     'count': cookie_dict['count']
    #     # }
    #
    #     # 正确的数据结构
    #     new_cart_dict[sku_id] = cookie_dict['count']
    #
    #     if cookie_dict['selected']:
    #         new_selected_add.append(sku_id)
    #     else:
    #         new_selected_rem.append(sku_id)
    # print(new_cart_dict)
    # # 根据新的数据结构，合并到redis
    # redis_conn = get_redis_connection('carts')
    # pl = redis_conn.pipeline()
    # print(pl.hgetall('carts_%s' % user.id))
    # pl.hmset('carts_%s' % user.id, new_cart_dict)
    # if new_selected_add:
    #     pl.sadd('selected_%s' % user.id, *new_selected_add)
    # if new_selected_rem:
    #     pl.srem('selected_%s' % user.id, *new_selected_rem)
    # pl.execute()
    #
    # # 删除cookies
    # response.delete_cookie('carts')
    #
    # return response
